package com.example.capsule_chat0322.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.capsule_chat0322.R;
import com.example.capsule_chat0322.adapter.ChatGroupFriendAdapter;
import com.example.capsule_chat0322.bean.ChatGroupFriendBean;
import com.example.capsule_chat0322.bean.UserInfoBean;
import com.example.capsule_chat0322.global.GlobalAssistMap;
import com.example.capsule_chat0322.interfaces.ChatGroupFriendItemOnClickInterface;
import com.example.capsule_chat0322.request.RoomRequest;
import com.example.capsule_chat0322.request.UserInfoRequest;

import java.util.List;

/**
 * @Author zhuzhiming
 * @Date 2022/5/19 0:45
 */
public class ChatGroupFriendActivity extends AppCompatActivity {

    private EditText search_text;
    private TextView search_friend;
    private TextView refresh_friend;
    private RecyclerView chat_group_friend_recycler_view;
    private List<ChatGroupFriendBean> chatGroupFriendBeanList;
    private ChatGroupFriendAdapter chatGroupFriendAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chat_group_friend);
        initView();//初始化UI控件
    }

    private void initView() {
        String roomName = getIntent().getStringExtra("roomName");
        chat_group_friend_recycler_view = findViewById(R.id.chat_group_friend_recycler_view);
        search_friend = findViewById(R.id.search_friend);
        search_text = findViewById(R.id.search_text);
        refresh_friend = findViewById(R.id.refresh_friend);
        refresh_friend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                doGetChatGroupFriend(roomName);
            }
        });

        search_friend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String content = search_text.getText().toString();
                if (content.equals("") || content.length() > 28) {
                    return;
                }

                doSearchGroupFriend(roomName, content);

            }
        });

        doGetChatGroupFriend(roomName);

    }

    private void doSearchGroupFriend(String roomName, String content) {

        RoomRequest.searchGroupFriend(new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(@NonNull Message message) {
                updateActivity(message);
                return true;
            }
        }), roomName, content);

    }

    private void doGetChatGroupFriend(String roomName) {

        RoomRequest.getChatRoomFriend(new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(@NonNull Message message) {
                updateActivity(message);

                return true;
            }
        }), roomName);

    }

    private void updateActivity(@NonNull Message message) {
        chatGroupFriendBeanList = (List<ChatGroupFriendBean>) message.obj;

        LinearLayoutManager layoutManager = new LinearLayoutManager(ChatGroupFriendActivity.this);
        chat_group_friend_recycler_view.setLayoutManager(layoutManager);
        chatGroupFriendAdapter = new ChatGroupFriendAdapter(chatGroupFriendBeanList);
        adapterAddListener(chatGroupFriendAdapter);
        chat_group_friend_recycler_view.setAdapter(chatGroupFriendAdapter);
    }


    public void adapterAddListener(ChatGroupFriendAdapter chatGroupFriendAdapter) {

        if (chatGroupFriendAdapter == null) {
            return;
        }

        chatGroupFriendAdapter.setFunctionItemOnClickInterface(new ChatGroupFriendItemOnClickInterface() {

            @Override
            public void onItemClick(RecyclerView parent, View view, String senderId) {
                if (senderId.equals(GlobalAssistMap.getObject("userId").toString())) {
                    return;
                }
                Intent intent = new Intent();
                final UserInfoBean[] userInfoBean = {(UserInfoBean) GlobalAssistMap.getObject(senderId + "Info")};
                if (userInfoBean[0] == null) {
                    UserInfoRequest.doGetUserInfoRequest(new Handler(new Handler.Callback() {
                        @Override
                        public boolean handleMessage(@NonNull Message message) {
                            userInfoBean[0] = (UserInfoBean) GlobalAssistMap.getObject(senderId + "Info");
                            intent.putExtra("senderId", Integer.parseInt(senderId));
                            intent.putExtra("senderAvatar", userInfoBean[0].getAvatarUrl());
                            intent.setClass(ChatGroupFriendActivity.this, UserInfoActivity.class);
                            startActivity(intent);
                            return true;
                        }
                    }), senderId);
                } else {
                    intent.putExtra("senderId", Integer.parseInt(senderId));
                    intent.putExtra("senderAvatar", userInfoBean[0].getAvatarUrl());
                    intent.setClass(ChatGroupFriendActivity.this, UserInfoActivity.class);
                    startActivity(intent);
                }

            }
        });
    }

}
